package com.group.itms.routes.data;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.group.itms.routes.utils.Convert;
import com.j256.ormlite.dao.ForeignCollection;
import com.j256.ormlite.field.DataType;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.field.ForeignCollectionField;
import com.j256.ormlite.table.DatabaseTable;

@DatabaseTable
public class BObject {

	public static final String TYPE = "type";
	public static final String IS_ACTIVE = "isActive";

	@DatabaseField(id = true)
	private int id;
	@DatabaseField
	private String objInd;
	@DatabaseField(foreign = true, foreignAutoRefresh = true, columnName = TYPE)
	private ObjectType type;
	@DatabaseField(foreign = true, foreignAutoRefresh = true)
	private BObject parent;
	@DatabaseField
	private String name;
	@DatabaseField(dataType = DataType.BYTE_ARRAY)
	private byte[] mainFile;
	@DatabaseField(dataType = DataType.BYTE_ARRAY)
	private byte[] mainPhoto;
	@DatabaseField
	private String text;
	@DatabaseField(columnName = IS_ACTIVE)
	private boolean isActive;
	@DatabaseField
	private Date insertTime;
	@DatabaseField
	private int creatorId;
	@DatabaseField
	private String color;

	@ForeignCollectionField
	private ForeignCollection<BObjectTopic> objectTopics;

	@ForeignCollectionField
	private ForeignCollection<ObjectGps> objectGpses;

	@ForeignCollectionField
	private ForeignCollection<BObjectOption> objectOptions;

	@ForeignCollectionField
	private ForeignCollection<BObject> children;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getObjInd() {
		return objInd;
	}

	public void setObjInd(String objInd) {
		this.objInd = objInd;
	}

	public ObjectType getType() {
		return type;
	}

	public void setType(ObjectType type) {
		this.type = type;
	}

	public BObject getParent() {
		return parent;
	}

	public void setParent(BObject parent) {
		this.parent = parent;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public byte[] getMainFile() {
		return mainFile;
	}

	public void setMainFile(byte[] mainFile) {
		this.mainFile = mainFile;
	}

	public byte[] getMainPhoto() {
		return mainPhoto;
	}

	public void setMainPhoto(byte[] mainPhoto) {
		this.mainPhoto = mainPhoto;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public boolean isActive() {
		return isActive;
	}

	public void setActive(boolean isActive) {
		this.isActive = isActive;
	}

	public Date getInsertTime() {
		return insertTime;
	}

	public void setInsertTime(Date insertTime) {
		this.insertTime = insertTime;
	}

	public int getCreatorId() {
		return creatorId;
	}

	public void setCreatorId(int creatorId) {
		this.creatorId = creatorId;
	}

	public boolean isLocation() {
		return is(ObjectType.LOCATION);
	}

	public boolean isRoute() {
		return is(ObjectType.ROUTE);
	}

	public boolean isTask() {
		return is(ObjectType.TASK);
	}

	public boolean isPoint() {
		return is(ObjectType.INF_POINT);
	}

	public List<Topic> getTopics() {
		List<Topic> topics = new ArrayList<Topic>();
		List<BObjectTopic> objTopics = Convert.toList(objectTopics);
		for (BObjectTopic ot : objTopics) {
			if (!ot.isActive()) {
				continue;
			}
			Topic tpc = ot.getTopic();
			if (tpc != null) {
				topics.add(tpc);
			}
		}
		return topics;
	}

	public List<Option> getOptions() {
		List<Option> options = new ArrayList<Option>();
		List<BObjectOption> objOptions = Convert.toList(objectOptions);
		for (BObjectOption oo : objOptions) {
			if (!oo.isActive()) {
				continue;
			}
			Option opt = oo.getOption();
			if (opt != null && opt.isActive()) {
				options.add(oo.getOption());
			}
		}
		return options;
	}

	public List<BObject> getChildrenOfType(ObjectType type) {
		List<BObject> filteredChildren = new ArrayList<BObject>();
		List<BObject> childrenLst = getChildren();
		for (BObject o : childrenLst) {
			if (o.getType().equals(type)) {
				filteredChildren.add(o);
			}
		}
		return filteredChildren;
	}

	public List<BObject> getChildren() {
		List<BObject> filteredChildren = new ArrayList<BObject>();
		List<BObject> childrenLst = Convert.toList(children);
		for (BObject o : childrenLst) {
			if (o.isActive) {
				filteredChildren.add(o);
			}
		}
		return filteredChildren;
	}

	public List<BObject> getChildrenRoutes() {
		return getChildrenOfType(ObjectType.ROUTE);
	}

	public List<BObject> getChildrenTasks() {
		return getChildrenOfType(ObjectType.TASK);
	}

	public List<BObject> getChildrenPoints() {
		return getChildrenOfType(ObjectType.INF_POINT);
	}

	public List<ObjectGps> getGpsObjects() {
		List<ObjectGps> gpsObjects = Convert.toList(objectGpses);
		List<ObjectGps> ogList = new ArrayList<ObjectGps>();
		for (ObjectGps og : gpsObjects) {
			if (og.isActive()) {
				ogList.add(og);
			}
		}
		return ogList;
	}

	public String getIndName() {
		return getIndName(" ");
	}

	public String getIndName(String beteween) {
		return (isNotNull(objInd) ? objInd : "") + beteween + name;
	}

	public String getNameInd() {
		return getNameInd(" ");
	}

	public String getNameInd(String between) {
		return name + between + (isNotNull(objInd) ? objInd : "");
	}

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}

	private boolean isNotNull(String s) {
		return s != null && !s.equals("null");
	}

	@Override
	public String toString() {
		return "BObject[name=" + name + ", type=" + type + "]";
	}

	@Override
	public boolean equals(Object o) {
		if (!(o instanceof BObject))
			return false;
		BObject obj = (BObject) o;
		return obj.getId() == this.getId();
	}

	public boolean is(ObjectType type) {
		return this.type != null ? this.type.equals(type) : type == null;
	}

}
